package cn.kim.edu.info.manager.service;

import cn.kim.edu.info.manager.dao.TeacherDao;
import cn.kim.edu.info.manager.domain.Teacher;

public class TeacherService {

    private TeacherDao teacherDao = new TeacherDao();

    //添加老师
    public boolean addTeacher(Teacher teacher) {
        return teacherDao.addTeacher(teacher);

    }

    //判断id是否在数组中已经存在
    public boolean isExists(String id) {
        //获取对象中的数组
        Teacher[] teachers = teacherDao.findAllTeacher();
        boolean exists = false;
        //遍历数组，取出每一个元素，进行判断
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    //逻辑层实现查看方法
    public Teacher[] findAllTeacher() {
        Teacher[] allTeacher = teacherDao.findAllTeacher();
        boolean flag = false;
        for (int i = 0; i < allTeacher.length; i++) {
            Teacher ts = allTeacher[i];
            if (ts != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            return allTeacher;
        } else {
            return null;
        }
    }

    //逻辑层的删除方法
    public void deleteTeacherById(String id) {
        teacherDao.deleteTeacherById(id);

    }

    //逻辑层修改方法
    public void updateTeacher(String id, Teacher newTeacher) {
        teacherDao.updateTeacher(id,newTeacher);
    }
}
